<?php
namespace app\admin\model\operating;
use think\facade\Db;
use app\admin\common;

class Activity{
	private $table_obj;
	
	public function findActivityData($_where,$_field)
	{
		extract(common::get('activity',[$_where,$_field,__function__]));
		if(empty($data)){
			$data=Db::name("activity")->alias("a")
				->join("opus_series s","s.id=a.series_id")
				->join("opus_series ss","ss.id=a.winning_series_id")
				->where($_where)
				->field($_field)
				->find();
			common::set($data);	
		}
		return $data;
	}
	
	public function selectActivityData($_where,$_field,$_limit,$_order)
	{
		extract(common::get('activity',[$_where,$_field,__function__]));
		if(empty($data)){
			$data=Db::name("activity")->alias("a")
				->join("opus_series s","s.id=a.series_id")
				->join("opus_series ss","ss.id=a.winning_series_id")
				->where($_where)
				->field($_field)
				->limit(current($_limit),end($_limit))
				->order($_order)
				->select();
		}
		return empty($data)?:$data->toArray();
	}
	
	public function insertActivity ($_data) 
	{
		common::delete('activity');	
		return Db::name('activity')->strict(false)->insert($_data);
	}
	
	public function upActivity($_up,$_where)
	{
		common::delete('activity');	
		return Db::name('activity')->where($_where)->update($_up);
	}
	
	public function findActivity($_where,$_field){
		return Db::name("activity")->where($_where)->field($_field)->find();
	}
	
	public function selectActivity($_where,$_field)
	{
		$_data=Db::name("activity")->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function findDetails ($_where,$_field)
	{
		extract(common::get('activity',[$_where,$_field,__function__]));
		if(empty($data)){
			$data=Db::name("activity")->where($_where)->field($_field)->find();
			if(empty($data)){
				return [];
			}
			common::set($data);
		}
		return $data;
	}
	
	public function selectActivityTotalPeople($_where,$_field,$_group)
	{
		$_data=Db::name("user_activity_sign_up")->where($_where)->field($_field)->group($_group)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function findWhite($_where,$_field){
		return Db::name("activity_white_list")->where($_where)->field($_field)->find();
	}
	
	public function findWhiteUser($_where,$_field){
		return Db::name("user")->alias("u")
			->leftJoin("activity_white_list w","w.mobile=u.mobile")
			->where($_where)
			->field($_field)
			->find();
	}
	public function findWhiteData($_where,$_field){
		return Db::name("activity_white_list")->alias("w")
			->join("user u","u.id=w.user_id")
			->where($_where)
			->field($_field)
			->find();
	}
	public function selectWhiteData($_where,$_field,$_limit,$_order){
		$_data=Db::name("activity_white_list")->alias("w")
			->join("user u","u.id=w.user_id")
			->where($_where)
			->limit(current($_limit),end($_limit))
			->order($_order)
			->field($_field)
			->select();
		return empty($_data)?:$_data->toArray();	
	}
	
	public function insertWhiteUser($_data){
		return Db::name("activity_white_list")->insert($_data);
	}
	
	
	public function upWhiteUser($_up,$_where)
	{
		return Db::name("activity_white_list")->where($_where)->update($_up);
	}
	
	public function selectActivityUserData($_where,$_field){
		$_data=Db::name("user_activity_sign_up")->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function selectUser($_where,$_field){
		$_data=Db::name("user")->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function upUser($_up,$_where)
	{
		return Db::name("user")->where($_where)->update($_up);
	}
	
	public function insertUserLuckyDraw($_data)
	{
		return Db::name("activity_user_lucky_draw")->insert($_data,true);
	}
	
	public function findLucklyData($_where,$_field){
		return Db::name("activity_user_lucky_draw")->alias("d")
			->join("user u","u.id=d.user_id")
			->where($_where)
			->field($_field)
			->find();
	}
	public function selectLucklyData($_where,$_field,$_limit,$_order){
		$_data=Db::name("activity_user_lucky_draw")->alias("d")
			->join("user u","u.id=d.user_id")
			->where($_where)
			->limit(current($_limit),end($_limit))
			->order($_order)
			->field($_field)
			->select();
		return empty($_data)?:$_data->toArray();	
	}
	public function selectLuckly($_where,$_field){
		$_data=Db::name("activity_user_lucky_draw")->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();	
	}
	public function findSeries($_where,$_field){
		return Db::name("opus_series")->where($_where)->field($_field)->find();
	}
}
?>